<?php
/**
 *
 * @author Rodrigo Régis Palmeira <regisbsb@gmail.com>
 * @version 0.1
 *
 */
namespace Foxforms;
class Element
{
	protected $name;
	protected $id;
	protected $type;
	protected $label;
	protected $class	= array();
	protected $style	= array();
	protected $event	= array();
	protected $element	= array();

	/**
	 * Set the type of a input
	 * @param string $value
	 * @return FoxInputForms
	 */
	public function setType($type)
	{
		$this->type = $type;
		return $this;
	}

	public function setLabel($label)
	{
		$this->label = $label;
		return $this;
	}

	public function get($attribute)
	{
		return $this->$attribute;
	}

	public function getElement($name)
	{
		return $this->element[$name];
	}

	/**
	 * Set the name attribute
	 * @param string $name
	 * @return FoxElementForm
	 */
	public function setName($name)
	{
		$this->name = $name;

		if (!isset($this->id))
		{
			$name = str_replace(array('[',']'),array('_',''), $name);
			$this->setId('id_'.$name);
		}
		return $this;
	}

	/**
	 * Set the class attribute
	 * @param string $id
	 * @return FoxElementForm
	 */
	public function setId($id)
	{
		$this->id = $id;
		return $this;
	}

	/**
	 * Add classes for the element
	 * @param array/string $class
	 * @return FoxElementForm
	 */
	public function addClass($class)
	{
		if (!is_array($class))
			$class = array($class);

		$this->class = array_merge($class, $this->class);
		return $this;
	}

	public function addStyle($style)
	{
		if (!is_array($style))
			$style = array($style);

		$this->style = array_merge($style, $this->style);
		return $this;
	}

	/**
	 * Add event's for the element
	 * @param array(event => action) $event
	 * @return FoxElementForm
	 */
	public function addEvent($event, $action)
	{
		$this->event[$event][]	= $action;
		return $this;
	}

	/**
	 * Add an element in a form
	 * @param FoxElementForms $element
	 * @return FoxFormForms
	 * @todo refatorar a parte de setar os valores default
	 */
	public function addElement(Element $element)
	{
		if (is_null($element->getValue()))
		{
			foreach ($this->values as $key => $value)
			{
				$findMe	= strpos($element->get('name'), $key);
				if ($findMe !== false)
				{
					$keyLenght	= strlen($key) +1;
					$name	= substr($element->get('name'),$keyLenght, strlen($element->get('name'))-$keyLenght-1);

					//SE O VALOR FOR OBJETO DEVEMOS CAPTURAR SUA PK
					if ($value->get($name) && is_object($value->get($name)) && $value->get($name) instanceof \DateTime)
					{
						$element->setValue($value->get($name)->format('d/m/Y'));
					}
					else if ($value->get($name) && is_object($value->get($name)))
					{
						$em = \Foxframe\Registry::get('entityManager');
						$meta = $em->getClassMetadata(get_class($value->get($name)));
						$identifier = $meta->getSingleIdentifierFieldName();
						$element->setValue($value->get($name)->get($identifier));
					}
					else
						$element->setValue($value->get($name));
				}
			}
		}

		$this->element[$element->get('name')] = $element;
		return $this;
	}

	public function getLabel()
	{
		if ($this->get('validate') == true)
			$this->setLabel("<i style='color:red'>*</i>&nbsp;".$this->get('label'));

		$label	= \Foxforms\Widget\Label::create($this->get('label'))->setFor($this->get('id'));

		return $label;
	}

	public function generateForm(\Doctrine_Table $table)
	{
		$form = new \stdClass();

		foreach ($table->getColumns() as $key => $param)
		{
			$form->$key = \Foxforms\Widget\Field::createField($key, $param);
		}

		return $form;
	}

	/**
	 * Render the html output
	 * @return string
	 */
	public function render()
	{
		$html	= ' ';

// 		echo '<pre>';print_r($this);die;

		if ($this instanceof \Foxforms\Widget\Form)
			$html	.= '<form';
		else if ($this instanceof \Foxforms\Widget\Field\Input)
			$html	.= '<input';
		else if ($this instanceof \Foxforms\Widget\Field\Select)
			$html	.= '<select';
		else if ($this instanceof \Foxforms\Widget\Field\TextArea)
			$html	.= '<textarea';
		else if ($this instanceof \Foxforms\Widget\Field\Div)
			$html	.= '<div';
		else if ($this instanceof \Foxforms\Widget\Label)
			$html	.= '<label';
		else if ($this instanceof \Foxforms\Widget\A)
			$html	.= '<a';
		else if ($this instanceof \Foxforms\Widget\Button)
			$html	.= '<button';
		else if ($this instanceof \Foxforms\Widget\Fieldset)
			$html	.= '<fieldset';


		if (!empty($this->name))
			$html	.= " name='{$this->name}'";
		if (!empty($this->type))
			$html	.= " type='{$this->type}'";
		if (!empty($this->value))
			$html	.= " value='{$this->value}'";
		if (!empty($this->maxlength))
			$html	.= " maxlength='{$this->maxlength}'";
		if (!empty($this->size))
			$html	.= " size='{$this->size}'";
		if (!empty($this->readOnly))
			$html	.= " readonly";
		if (!empty($this->disabled))
			$html	.= " disabled='{$this->disabled}'";
		if (!empty($this->id))
			$html	.= " id='{$this->id}'";
		if (!empty($this->for))
			$html	.= " for='{$this->for}'";
		if (!empty($this->action))
			$html	.= " action='{$this->action}'";
		if (!empty($this->method))
			$html	.= " method='{$this->method}'";
		if (!empty($this->target))
			$html	.= " target='{$this->target}'";
		if (!empty($this->enctype))
			$html	.= " enctype='{$this->enctype}'";
		if (!empty($this->multiple))
			$html	.= " multiple='{$this->multiple}'";
		if (!empty($this->cols))
			$html	.= " cols='{$this->cols}'";
		if (!empty($this->rows))
			$html	.= " rows='{$this->rows}'";
		if (!empty($this->href))
			$html	.= " href='{$this->href}'";
		if (!empty($this->placeHolder))
			$html	.= " placeholder='{$this->placeHolder}'";

		if (count($this->class))
		{
			$class = implode(' ',$this->class);
			$html.= " class='{$class}'";
		}

		if (count($this->style))
		{
			$style = implode('; ',$this->style);
			$html.= " style=\"{$style}\"";
		}

		if (count($this->event))
		{
			foreach ($this->event as $event => $action)
			{
				$action = implode(';',$action);
				$html.= " {$event}=\"{$action}\"";
			}
		}
		$html	.= '>';

// 		foreach ($this->element as $element)
// 		{
// 			$html.= $element->render();
// 		}

		if ($this instanceof \Foxforms\Widget\Field\Select)
		{
			if (isset($this->placeHolder) && !($this->multiple))
			{
				$selected	= !isset($this->selected) ? "selected" : null;
				$html.= "<option {$selected} value=''>{$this->placeHolder}</option>";
			}
			$html.= $this->renderSelectOption($this->option);
		}

		if (!empty($this->text))
			$html	.= "{$this->text}";

		if (!empty($this->content))
			$html	.= "{$this->content}";

		if ($this instanceof \Foxforms\Widget\Field\Select)
			$html	.= '</select>';
		else if ($this instanceof \Foxforms\Widget\Field\TextArea)
			$html	.= '</textarea>';
		else if ($this instanceof \Foxforms\Widget\Form)
			$html	.= '</form>';
		else if ($this instanceof \Foxforms\Widget\Div)
			$html	.= '</div>';
		else if ($this instanceof \Foxforms\Widget\Label)
			$html	.= '</label>';
		else if ($this instanceof \Foxforms\Widget\A)
			$html	.= '</a>';
		else if ($this instanceof \Foxforms\Widget\Button)
			$html	.= '</button>';
		else if ($this instanceof \Foxforms\Widget\Fieldset)
			$html	.= '</fieldset>';
		else if ($this instanceof \Foxforms\Widget\Legend)
			$html	.= '</legend>';

		return $html;
	}

	public function renderSelectOption($option)
	{
		$html	= null;
		foreach ($option as $key => $value)
		{
			if (is_array($value))
			{
				$html.= "<optgroup label='{$key}'>";
				$html.= $this->renderSelectOption($value);
				$html.= "</optgroup>";
			}
			else
			{
				$selected = null;
				if (is_array($this->selected) && in_array($key, $this->selected))
					$selected = "selected='selected'";
				else if ($this->selected == $key)
					$selected = "selected='selected'";

				$html.= "<option value='{$key}' {$selected}>{$value}</option>";
			}
		}
		return $html;
	}

}